package com.toolkit.util;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 * @author qdl
 * @descripton TODO
 * @Date 2025/3/26 14:39
 */
public class IDCardUtils {
    public static String getBirthDateFromID(String idCard) {
        if (idCard == null || (idCard.length() != 18 && idCard.length() != 15)) {
            return "无效的身份证号码";
        }

        String birthDateStr;
        if (idCard.length() == 18) {
            birthDateStr = idCard.substring(6, 14); // 获取 7-14 位
        } else {
            birthDateStr = "19" + idCard.substring(6, 12); // 15 位身份证补充 "19"
        }

        try {
            // 解析成日期格式
            LocalDate birthDate = LocalDate.parse(birthDateStr, DateTimeFormatter.ofPattern("yyyyMMdd"));
            return birthDate.toString(); // 返回格式：YYYY-MM-DD
        } catch (Exception e) {
            return "身份证号码错误";
        }
    }
}
